package de.dhbw.karlsruhe.picsim.model.timer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Created by IntelliJ IDEA.
 * User: goesen
 * Date: Apr 22, 2010
 * Time: 11:24:26 PM
 */
final class Watchdog extends TimerSupport implements ResetAbleTimer {
    private static final Log log = LogFactory.getLog(Watchdog.class);
    private int maxValue;
    private int counter = 0;

    Watchdog(final int maxValue) {
        if (maxValue > 0) {
            this.maxValue = maxValue;
        } else {
            throw new IllegalArgumentException("maxValue is" + maxValue);
        }
    }


    @Override
    public void reset() {
        log.info("watchdog was reseted on Value=" + counter);
        counter = 0;
    }

    @Override
    public void increment() {
        counter++;
        if (counter >= maxValue) {
            log.info("watchdog timed out");
            fireTimeout();
            // eigentlich geht nach nem timeout ja sowieso nichts mehr.... aber machen wir einfach mal weiter
            //reset Watchdog, nicht den Pic Prozesoor ;) 
            reset();
        }
    }

}
